/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.controler;

import client.model.ShipCounter;
import client.view.MainForm;
import client.view.RepaintNotifier;

import java.awt.Point;
import java.awt.event.MouseEvent;

import sharedObjects.Field;
import sharedObjects.Ships;

/**
 * 
 * @author Niclas
 */
public class ClickHandler {

	MainForm mf;
	boolean shipInBuild = false;
	int lastIndexOfShipStorage = 0;
	int selectedShip = -1;
	int maximumFieldForActualShip;
	boolean isShipAvailableForActualShipType;
	private static ClickHandler instance = null;

	public static ClickHandler getInstance(MainForm mf) {
		if (instance == null) {
			instance = new ClickHandler(mf);
		}
		return instance;
	}

	private ClickHandler(MainForm mf) {
		this.mf = mf;
	}

        public void shoot(MouseEvent e){
            LaunchController lc = LaunchController.getInstance();
            Field f = lc.fieldStorageEnemy.getFieldByCoordinates(e.getPoint());
            f.setHitten(true);
            RepaintNotifier rpn = RepaintNotifier.getInstance();
			rpn.doRepaint();
        }
        
	public void shipSegmentPlaced(MouseEvent e) {
		if (selectedShip != -1) {
			ShipCounter sc = ShipCounter.getInstance();
			Point p = e.getPoint();
			LaunchController lc = LaunchController.getInstance();
			Field f = lc.fieldStorage.getFieldByCoordinates(p);
			if (f != null) {
				if (/*!fieldInUse(f)*/true) {
					if (selectedShip == Ships.TYPE_KREUZER) {
						maximumFieldForActualShip = ShipCounter.DAFAULT_KREUZER_LENGHT;
						isShipAvailableForActualShipType = sc
								.isKreuzerAvailable();
					} else if (selectedShip == Ships.TYPE_SCHLACHTSCHIFF) {
						maximumFieldForActualShip = ShipCounter.DAFAULT_SCHLACHTSCHIFF_LENGHT;
						isShipAvailableForActualShipType = sc
								.isSchlachtschiffAvailable();
					} else if (selectedShip == Ships.TYPE_ZERSTOERER) {
						maximumFieldForActualShip = ShipCounter.DAFAULT_ZERSTOERER_LENGHT;
						isShipAvailableForActualShipType = sc
								.isZerstoererAvailable();
					} else if (selectedShip == Ships.TYPE_UBOOTE) {
						maximumFieldForActualShip = ShipCounter.DAFAULT_UBOOT_LENGHT;
						isShipAvailableForActualShipType = sc
								.isUbootAvailable();
					}

					if (!shipInBuild) {
						if (isShipAvailableForActualShipType) {
							Ships s = new Ships();
							s.shipType = selectedShip;
							lc.shipStorage.ships.add(s);
							lastIndexOfShipStorage = lc.shipStorage.ships
									.size() - 1;
							if (selectedShip == Ships.TYPE_KREUZER) {
								sc.subtractKreuzer();
							} else if (selectedShip == Ships.TYPE_SCHLACHTSCHIFF) {
								sc.subtractSchlachtschiff();
							} else if (selectedShip == Ships.TYPE_ZERSTOERER) {
								sc.subtractZerstoerer();
							} else if (selectedShip == Ships.TYPE_UBOOTE) {
								sc.subtractUboot();
							}

							shipInBuild = true;
							mf.disableOtherToggleButtons(selectedShip);
						}
					}

					if (lc.shipStorage.ships.get(lastIndexOfShipStorage).shipfieldList
							.size() <= maximumFieldForActualShip - 1) {
						lc.shipStorage.ships.get(lastIndexOfShipStorage).shipfieldList
								.add(f);
						RepaintNotifier rpn = RepaintNotifier.getInstance();
						rpn.doRepaint();
					}
					if (lc.shipStorage.ships.get(lastIndexOfShipStorage).shipfieldList
							.size() == maximumFieldForActualShip) {
						mf.unselectAllToggleButtons();
						shipInBuild = false;
						selectedShip = -1;
						mf.enableAllToggleButtons();
					}

				}
			}
		}

	}

	public boolean fieldInUse(Field f) {
		LaunchController lc = LaunchController.getInstance();
		for (int i = 0; i < lc.shipStorage.ships.size(); i++) {
			for (int j = 0; i < lc.shipStorage.ships.get(i).shipfieldList.size(); j++){
				int osID = lc.shipStorage.ships.get(i).shipfieldList.get(j).getId();
				int nsID = f.getId();
				if (osID == nsID){
					return true;
				}
				
			}
		}
		return false;
	}

	public void shipSchlachtschiff5Selected() {
		selectedShip = Ships.TYPE_SCHLACHTSCHIFF;
		System.out.println("5er Ship Selected");
	}

	public void shipKreuzer4Selected() {
		selectedShip = Ships.TYPE_KREUZER;
		System.out.println("4er Ship Selected");
	}

	public void shipZerstoererSelected() {
		selectedShip = Ships.TYPE_ZERSTOERER;
		System.out.println("3er Ship Selected");
	}

	public void shipUbootSelected() {
		selectedShip = Ships.TYPE_UBOOTE;
		System.out.println("2er Ship Selected");
	}

}
